////// 2031 User's Manual - Part 2 \\\\\\\ Chapter 6 ADVANCED FILE MANAGEMENT In the preceding chapters, you learned how to manipulate files on the disk, and were shown the format of commands used to create and update files. In this chapter, you will utilize these skills in a file handling application using random or relative access. SPECIAL OPEN AND CLOSE STATEMENTS FOR DIRECT ACCESS The BASIC statements (after initializing the disk): OPEN2,8,4,"#" and OPEN2,8,4,"#0" are examples that show how to open a channel to one buffer (to be used with the block commands). In the first example, the first available buffer is allocated to channel 4. The second example is an attempt to allocate buffer 0 to the channel. 55 If the buffers are not available, a NO CHANNEL error condition is generated. The explicit buffer allocation can be used to reserve a buffer for position dependent code as in the case of a EXECUTE command. Execute a GET# statement to find the number of the allocated buffer. The byte transmitted is the buffer number. A buffer number may only be obtained PRIOR TO any write or read operations to that buffer. The CLOSE statement clears the OPENed channel and writes the BAM to the diskette that was last used by that channel. To avoid confusion, limit yourself to accessing one drive with any direct access channel. RANDOM ACCESS EXAMPLE Since the BLOCK-ALLOCATE command returns the next available diskette block through the error channel, it can be used in the allocation of records. This feature allows creating a random file without being concerned with the actual physical structure of the diskette. However, the allocated blocks must first be recorded in a sequential or user file in order to be referenced by the BASIC program. The following random file example demonstrates the use of block access com- mands. Notice that the U1 and U2 commands are used. These commands are used since more than one record is stored in a block and it is necessary to manage end-of-record pointers in BASIC. A smaller application might take advantage of the B-R and B-W commands. Chapter 9 contains a complete listing of the random access program entitled "RANDOMEXAMPLE". The example program is built upon a relative record scheme and provides single record access through BASIC programming. Most of the programming below line 20000 is relative record access. The field accessing routines left-justify binary and alpha fields, and right-justify numeric fields. In an actual situation, the program should generate error messages to the opera- tor, or automatically take corrective action such as rounding numbers to fit a field. It would also be possible to add data sorts and searches as well as key fields to the program. Record size, including field markers, must be less than 254 characters. Field size is restricted to 80 characters because of the restrictions of the BASIC INPUT# statement. Longer fields could be used if the BASIC pro- gram were modified to use GET# for retrieval but that procedure would be much slower. 56 Two sequential files are used to support the random access file in this example. Each file bears the name of the file name given in the CREATE file code (lines 1100 to 1180) plus a six-character extension. Since primary file names are ten or less characters, the file names are padded with spaces. The two files are named FILENAME .DESCR and FILENAME .KEY01. The descriptor ( .DESC) contains information about record structure and location. The primary key file ( .KEY01) contains the first field of each record and the relative record number. This example allows the random records to reside on a separate diskette from the sequential support files, thereby providing added room for random data. The OPEN code (lines 1200 to 1275) requires the disk ID of the random file disk for comparison. NOTE The DOS Support Program must not be in the computer when running this program. To Create A File: STEP 1. Insert the Demo Disk. STEP 2. Type: LOAD"RANDOM*",8 and press RETURN This loads the Random Program STEP 3. Type: OPEN 15,8,15 and press RETURN This opens the command channel. STEP 4. Remove the Demo Disk and insert a blank disk. STEP 5. Type: PRINT#15,"NO:MAILING LIST,CS" and press RETURN STEP 6. Type: RUN and press RETURN The screen diskplays: "DO YOU WISH TO CREATE A FILE"? STEP 7. Type: Y and press RETURN The screen displays: "RANDOM FILE NAME"? STEP 8. Type the file name: PHONE LIST and press RETURN The screen displays: "KEY FILE DRIVE NUMBER"? STEP 9. Type: 0 and press RETURN The screen displays: "RANDOM FILE NUMBER"? 57